﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Core.Dtos;
using Core.Messages;
using Core.Queries;
using Core.IRepository;
using Core.Domain;
using Library.Helpers;
using Front.Controllers.Api;
using System.IO;
using System.Collections;
using Core.IServices;
using Library.Logging;
using Core.Factories;
using Core.Context;
using System.Web.Http.Cors;

namespace Front.Areas.Admin.Controllers.Api
{
    [RoutePrefix("api/admin/dictionaries/{dictionaryId}/items")]
    //[Authorize(Roles = "Admin")]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class DictionaryItemApiController : ApiController
    {
        private readonly IManageDictionaryItemService dictionaryItemService;
        private readonly IRequestFactory requestFactory;

        public DictionaryItemApiController(IManageDictionaryItemService dictionaryItemService, ILogger Logger, ICurrentContext currentContext, IRequestFactory requestFactory)
        {
            this.dictionaryItemService = dictionaryItemService;
            this.requestFactory = requestFactory;
        }

        [Route("searches")]
        [HttpPost]
        public QueryResponse<DictionaryItemDto> FindBy(int dictionaryId)
        {
            var request = requestFactory.GetInstance<DictionaryItemQuery>(new DictionaryItemQuery() { DictionaryID = dictionaryId });
            return dictionaryItemService.FindBy(request);
        }

        [Route("{itemId}")]
        [HttpGet]
        public DictionaryItemDto Get(int dictionaryId, int itemId)
        {
            return dictionaryItemService.FindByID(itemId);
        }

        [HttpPost]
        [Route("")]
        public Response<int> Post(int dictionaryId, DictionaryItemDto itemDto)
        {
            itemDto.DictionaryID = dictionaryId;
            var request = requestFactory.GetInstance<DictionaryItemDto>(itemDto);
            return dictionaryItemService.Add(request);
        }

        [HttpPut]
        [Route("{itemId}")]
        public Response Put(int dictionaryId, int itemId, DictionaryItemDto itemDto)
        {
            itemDto.DictionaryID = dictionaryId;
            var request = requestFactory.GetInstance<DictionaryItemDto>(itemDto);
            return dictionaryItemService.Edit(itemId, request);
        }

        [HttpDelete]
        [Route("{itemId}")]
        public Response Delete(int dictionaryId, int itemId)
        {
            return dictionaryItemService.Delete(itemId);
        }
    }
}
